package com.yunduo.world.worker;

import com.yunduo.world.constant.NetWorkConstant;
import com.yunduo.world.constant.NetWorkRoomConstant;
import com.yunduo.world.entity.EntityInfo;
import com.yunduo.world.factory.ChannelWorker;
import com.yunduo.world.utils.ClientUtils;
import com.yunduo.world.utils.EntityBuild;
import lombok.extern.slf4j.Slf4j;

/**
 * 处理登录数据
 */
@Slf4j
public class LoginWorker extends ChannelWorker {
    @Override
    public void execute() {
        String id = ClientUtils.get(getCtx().channel());
        EntityInfo.User user = getMsg().getUser();
        if ( null != user) {
            log.info("客户端{} 上线用户 {}",id,user.getName());
            ClientUtils.put(getCtx().channel(),user.getName());
        }
        getCtx().writeAndFlush(buildMessage(NetWorkConstant.LOGIN_OK,NetWorkConstant.OK));
    }

    public EntityInfo.Entity buildMessage(String context,boolean status){
        return EntityBuild.buildMessage(NetWorkConstant.LOGIN,context,status);
    }

    public EntityInfo.Entity buildMessage(String context,boolean status,byte[] bytes){
        return EntityBuild.buildMessage(NetWorkConstant.LOGIN,context,status,bytes);
    }
}
